package cn.zz.veggie.application.packer;

import java.util.List;

import cn.zz.veggie.model.domain.OrderInfo;
import cn.zz.veggie.model.domain.Product;
import cn.zz.veggie.model.domain.Purchase;
import cn.zz.veggie.model.domain.Shop;
import cn.zz.veggie.web.types.vo.mgmt.purchase.MgmtPurchaseDetailVo;
import cn.zz.veggie.web.types.vo.mgmt.purchase.MgmtPurchaseDetailVo.MgmtPurchaseDetailItemVo;
import cn.zz.veggie.web.types.vo.mgmt.purchase.MgmtPurchaseVo;

/**
 * @author zhongq
 * @since 2024/6/8
 */
public class PurchasePacker {

	public static MgmtPurchaseVo toMgmtPurchaseVo(Purchase purchase) {
		final MgmtPurchaseVo vo = new MgmtPurchaseVo();
		vo.setId(purchase.getId());
		vo.setOrderCount(purchase.getOrderCount());
		vo.setProductCount(purchase.getProductCount());
		vo.setStatTime(purchase.getStatTime());
		vo.setStatus(purchase.getStatus());
		vo.setRemark(purchase.getRemark());
		vo.setCreatedAt(purchase.getCreatedAt());
		vo.setUpdatedAt(purchase.getUpdatedAt());
		return vo;
	}

	public static MgmtPurchaseDetailItemVo toMgmtPurchaseDetailItemVo(OrderInfo orderItem, Shop shop) {
		MgmtPurchaseDetailItemVo vo = new MgmtPurchaseDetailItemVo();
		vo.setId(orderItem.getId());
		vo.setShop(ShopPacker.toIdName(shop));
		vo.setQuantity(orderItem.getQuantity());
		vo.setShipped(orderItem.getShipped());
		vo.setPrice(orderItem.getPrice());
		vo.setAmount(orderItem.getAmount());
		vo.setRemark(orderItem.getRemark());
		return vo;
	}

	public static MgmtPurchaseDetailVo toMgmtPurchaseDetailVo(Product product, List<MgmtPurchaseDetailItemVo> itemVos) {
		MgmtPurchaseDetailVo vo = new MgmtPurchaseDetailVo();
		final int totalShipped = itemVos.stream().mapToInt(MgmtPurchaseDetailItemVo::getShipped).sum();
		vo.setProduct(ProductPacker.toHisProductVo(product));
		vo.setQuantity(totalShipped);
		vo.setItems(itemVos);
		return vo;
	}
}